package com.inet.receiver;

import java.util.HashMap;
import java.util.Map;

import com.inet.activity.WarningActivity;
import com.inet.util.HttpUtil;
import com.inet.util.L;
import com.inet.util.Utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

public class MyHandler extends Handler {

	public static final int LOWPOWERSTATUS = 0;
	public static final int CLOSELAUNCHER = 1;
	public static final int OPENLAUNCHER = 2;
	public static final int STARTVIDEO = 3;
	public static final int STOPVIDEO = 4;
	public static final int DOWNLOADVIDEOCOMPLETE = 5;

	private Context mContext;

	public MyHandler(Context context) {
		mContext = context;
	}

	@Override
	public void handleMessage(Message msg) {
		super.handleMessage(msg);
		switch (msg.what) {
		case LOWPOWERSTATUS:
			// 开启工作线程，反馈低电状态给服务器
			new Thread(new Runnable() {

				@Override
				public void run() {
					try {
						Log.i("TAG",
								"-----------send  LOWPOWER to server----------");
						Map<String, String> map = new HashMap<String, String>();
						map.put("imei", Utils.getImei(mContext));
						HttpUtil.sendPost(map,
								Utils.getPropertiesValue("post_lowPower"));
					} catch (Exception e) {
					}
				}
			}).start();
			break;
		case CLOSELAUNCHER:
			// 警告模式，持续打开WarningActivity
			try {

				removeMessages(CLOSELAUNCHER);
				if (Settings.System.getInt(mContext.getContentResolver(),
						"is_user_lock", 0) == 1) {
					Intent warningIntent = new Intent(mContext,
							WarningActivity.class);
					warningIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					mContext.startActivity(warningIntent);
					sendEmptyMessageDelayed(1, 2000);
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
			break;
		case OPENLAUNCHER:
			try {

				removeMessages(CLOSELAUNCHER);
				Intent mHomeIntent = new Intent(Intent.ACTION_MAIN);

				mHomeIntent.addCategory(Intent.CATEGORY_HOME);
				mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
						| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
				mContext.startActivity(mHomeIntent);
			} catch (Exception e) {
				// TODO: handle exception
			}
			break;
		case STARTVIDEO:
			try {
				L.i("receiver msg start video");
				String fileName = Settings.System.getString(
						mContext.getContentResolver(), "ConsoleServicesPath");
				L.i("Myhandler fileName==" + fileName);
				Intent VideoIntent = new Intent(Intent.ACTION_MAIN);
				VideoIntent.setClassName("com.example.videotest",
						"com.example.videotest.SurfaceViewTestActivity");
				VideoIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				VideoIntent.putExtra("path", fileName);
				mContext.startActivity(VideoIntent);
			} catch (Exception e) {
				// TODO: handle exception
			}
			break;
		case DOWNLOADVIDEOCOMPLETE:
			try {

				String savePath2 = Environment.getExternalStorageDirectory()
						+ "/ConsoleservicesVideo/";
				int i2 = HttpUtil.VideoPath.lastIndexOf("/");
				String fileName2 = savePath2
						+ HttpUtil.VideoPath.substring(i2 + 1);
				Settings.System.putString(mContext.getContentResolver(),
						"ConsoleServicesPath", fileName2);
				L.i("Myhandler fileName=="
						+ Settings.System.getString(
								mContext.getContentResolver(),
								"ConsoleServicesPath"));
				Toast.makeText(mContext, "广告下载完成", 1).show();
			} catch (Exception e) {
				// TODO: handle exception
			}
			break;
		default:
			break;
		}
	}
}
